package cn.cdutetc.library.controller;

import cn.cdutetc.library.common.ApiResponse;
import cn.cdutetc.library.entity.enums.BookStatusEnum;
import cn.cdutetc.library.entity.pojo.BookItem;
import cn.cdutetc.library.service.BookItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图书条目管理控制器
 */
@RestController
@RequestMapping("/api/book-items")
@Tag(name = "图书条目管理", description = "图书条目状态管理相关接口")
public class BookItemController {

    @Autowired
    private BookItemService bookItemService;

    @GetMapping("/book/{bookId}")
    @Operation(summary = "获取图书的所有条目", description = "根据图书ID获取所有图书条目")
    public ApiResponse<List<BookItem>> getBookItemsByBookId(@PathVariable Integer bookId) {
        List<BookItem> bookItems = bookItemService.getBookItemsByBookId(bookId);
        return ApiResponse.success(bookItems);
    }

    @GetMapping("/book/{bookId}/status/{status}")
    @Operation(summary = "根据状态获取图书条目", description = "根据图书ID和状态获取图书条目")
    public ApiResponse<List<BookItem>> getBookItemsByStatus(
            @PathVariable Integer bookId,
            @PathVariable String status) {
        List<BookItem> bookItems = bookItemService.getBookItemsByBookIdAndStatus(bookId, status);
        return ApiResponse.success(bookItems);
    }

    @PutMapping("/{itemId}/status")
    @Operation(summary = "更新图书条目状态", description = "更新单个图书条目的状态")
    public ApiResponse<Boolean> updateBookItemStatus(
            @PathVariable Integer itemId,
            @Parameter(description = "新状态") @RequestParam String status,
            @Parameter(description = "备注") @RequestParam(required = false) String remark) {
        boolean result = bookItemService.updateBookItemStatus(itemId, status, remark);
        return ApiResponse.success(result);
    }

    @PutMapping("/batch/status")
    @Operation(summary = "批量更新图书条目状态", description = "批量更新多个图书条目的状态")
    public ApiResponse<Boolean> batchUpdateBookItemStatus(@RequestBody BookItemStatusUpdateDTO updateDTO) {
        boolean result = bookItemService.batchUpdateBookItemStatus(
                updateDTO.getItemIds(), updateDTO.getStatus(), updateDTO.getRemark());
        return ApiResponse.success(result);
    }

    @GetMapping("/book/{bookId}/available")
    @Operation(summary = "获取可借阅的图书条目", description = "获取指定数量的可借阅图书条目")
    public ApiResponse<List<BookItem>> getAvailableBookItems(
            @PathVariable Integer bookId,
            @Parameter(description = "需要的数量") @RequestParam(defaultValue = "1") Integer count) {
        List<BookItem> bookItems = bookItemService.getAvailableBookItems(bookId, count);
        return ApiResponse.success(bookItems);
    }

    @GetMapping("/book/{bookId}/status-count")
    @Operation(summary = "获取图书条目状态统计", description = "获取图书各状态条目的数量统计")
    public ApiResponse<Map<String, Object>> getBookItemStatusCount(@PathVariable Integer bookId) {
        Map<String, Integer> statusCount = bookItemService.getBookItemStatusCount(bookId);

        // 转换为包含状态描述的响应
        Map<String, Object> result = new HashMap<>();
        for (Map.Entry<String, Integer> entry : statusCount.entrySet()) {
            Map<String, Object> statusInfo = new HashMap<>();
            statusInfo.put("count", entry.getValue());
            statusInfo.put("desc", BookStatusEnum.getByCode(entry.getKey()) != null ?
                    BookStatusEnum.getByCode(entry.getKey()).getDesc() : entry.getKey());
            result.put(entry.getKey(), statusInfo);
        }

        return ApiResponse.success(result);
    }

    @GetMapping("/status-options")
    @Operation(summary = "获取状态选项", description = "获取所有可用的图书状态选项")
    public ApiResponse<List<Map<String, String>>> getStatusOptions() {
        List<Map<String, String>> statusOptions = new java.util.ArrayList<>();
        for (BookStatusEnum status : BookStatusEnum.values()) {
            Map<String, String> option = new HashMap<>();
            option.put("code", status.getCode());
            option.put("desc", status.getDesc());
            statusOptions.add(option);
        }
        return ApiResponse.success(statusOptions);
    }

    /**
     * 图书条目状态更新DTO
     */
    static class BookItemStatusUpdateDTO {
        private List<Integer> itemIds;
        private String status;
        private String remark;

        // Getters and Setters
        public List<Integer> getItemIds() {
            return itemIds;
        }

        public void setItemIds(List<Integer> itemIds) {
            this.itemIds = itemIds;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getRemark() {
            return remark;
        }

        public void setRemark(String remark) {
            this.remark = remark;
        }
    }
}

